package co.com.jtune.view.main;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.jdesktop.application.ResourceMap;

import co.com.jtune.JTuneApp;
import co.com.jtune.controller.LibraryController;
import co.com.jtune.libraries.BusinessException;
import co.com.jtune.services.ServiceFactory;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.looks.BorderStyle;
import com.jgoodies.looks.HeaderStyle;
import com.jgoodies.looks.Options;
import com.jgoodies.looks.plastic.PlasticLookAndFeel;

public class MenuBarPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	public final String LIBRARY_CHANGE = "LIBRARY_CHANGE";
	public final static Dimension MENU_DIMENSION = new Dimension(250, 22);
	
	private JMenuBar menuBar;
	
	private JMenu fileMenu;
	private JMenuItem fileNewList;
	private JMenuItem fileAddTrack;
	private JMenuItem fileAddFolder;
	private JMenuItem fileExit;
	
	private JMenu editMenu;
	private JMenuItem editDeleteTrack;
	
	private ResourceMap resource;
	
	public MenuBarPanel(ResourceMap resource)
	{
		this.resource = resource;
		buildComponets();
		createSimpleGui();
	}
	
	private void buildComponets()
	{
		menuBar = new JMenuBar();
		menuBar.putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.SINGLE);
		menuBar.putClientProperty(PlasticLookAndFeel.BORDER_STYLE_KEY, BorderStyle.SEPARATOR);
		
		fileMenu = new JMenu(resource.getString("file.text"));
		fileMenu.setMnemonic(resource.getString("file.Mnemonic").charAt(0));		
		buildFileMenu();
		
		editMenu = new JMenu(resource.getString("edit.text"));
		editMenu.setMnemonic(resource.getString("edit.Mnemonic").charAt(0));		
		buildEditMenu();

		menuBar.add(fileMenu);
		menuBar.add(editMenu);
	}
	private void buildEditMenu() 
	{
		editDeleteTrack = new JMenuItem(resource.getString("edit.deleteTrack"));
		editDeleteTrack.setMnemonic(resource.getString("edit.deleteTrack.Mnemonic").charAt(0));
		editDeleteTrack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
		editDeleteTrack.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				deleteTrack();
			}
		});
		
		editMenu.add(editDeleteTrack);
	}

	private void buildFileMenu()
	{
		fileNewList = new JMenuItem(resource.getString("file.newList"));
		fileNewList.setMnemonic(resource.getString("file.newList.Mnemonic").charAt(0));
		fileNewList.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
		fileNewList.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				newList();
			}
		});
		
		fileAddTrack = new JMenuItem(resource.getString("file.addTrack"));
		fileAddTrack.setMnemonic(resource.getString("file.addTrack.Mnemonic").charAt(0));
		fileAddTrack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
		fileAddTrack.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				addTrack();
			}
		});
		
		fileAddFolder = new JMenuItem(resource.getString("file.addFolder"));
		fileAddFolder.setMnemonic(resource.getString("file.addFolder.Mnemonic").charAt(0));
		fileAddFolder.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
		fileAddFolder.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				addFolder();
			}
		});
		
		fileExit = new JMenuItem(resource.getString("file.exit"));
		fileExit.setMnemonic(resource.getString("file.exit.Mnemonic").charAt(0));
		fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
		fileExit.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				exit();
			}
		});
		
		fileExit.setPreferredSize(MENU_DIMENSION);
		
		fileMenu.add(fileNewList);
		fileMenu.add(new JSeparator());
		fileMenu.add(fileAddTrack);
		fileMenu.add(fileAddFolder);
		fileMenu.add(new JSeparator());
		fileMenu.add(fileExit);
	}
	
	private void newList()
	{
		System.out.println("newList");
	}
	
	private void addTrack()
	{
		System.out.println("addTrack");
		
		File[] files = openFileChooser();
		
		if (files != null)
		{
			try
			{
				LibraryController libraryController = ServiceFactory.getInstance().getLibraryController();
				libraryController.addTrackListToLibrary(files);
				
				firePropertyChange(LIBRARY_CHANGE, null, null);
			}catch(BusinessException be)
			{
				System.out.println(be.getExceptionMessage());
			}
		}
	    
	}
	
	private File[] openFileChooser() {
		
		JFileChooser chooser = new JFileChooser();
	    FileNameExtensionFilter filter = new FileNameExtensionFilter(
	        "", "mp3");
	    chooser.setFileFilter(filter);
	    chooser.setMultiSelectionEnabled(true);
	    int returnVal = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(this));
	    
	    if(returnVal != JFileChooser.APPROVE_OPTION) {
	    	return null;
	    }
	    
		return chooser.getSelectedFiles();
	}
	
	private void deleteTrack() {
		System.out.println("deleteTrack");
		
	}

	private void addFolder()
	{
		System.out.println("addFolder");
	}
	
	private void exit()
	{
		JTuneApp.getApplication().exit();
	}
	
	private void createSimpleGui()
	{
		FormLayout menuBarPanelLayout = new FormLayout( "pref:grow", "pref");		
		setLayout(menuBarPanelLayout);
		CellConstraints cc = new CellConstraints();
		add(menuBar, cc.xywh(1, 1, 1, 1, CellConstraints.FILL, CellConstraints.FILL));
	}
	

}
